import Reflux from 'reflux';
import StateMixin from 'reflux-state-mixin';
import ServiceListAction from 'pages/servicelist/actions/serviceListAction.js';
import API from 'config/api';
export default Reflux.createStore({
  mixins:[StateMixin.store],
  listenables:[ServiceListAction],
  getInitialState() {
    return {
      srvTree:[],
      info:{},
      selectedRows:[],
      srvCatTreeId:null
    };
  },
  onGetSrvList() {
    $.get(API.GET_SRVLIST, (data) => {
      this.setState({
        srvTree: data.data
      });
    });
  },
  onGetSrvCatBaseInfoById(id) {
    $.get(API.GET_SRVCATBASEINFO_BY_ID(id), (data) => {
      this.setState({
        info: data.data,
        selectedRows:data.data.srvItems
      });
    });
  },
  onCreateSrvCatDir(filter) {
    $.ajax({
      url: API.CREATE_SRVCATDIR,
      type:'post',
      data: JSON.stringify(filter),
      datatype:'json',
      contentType: 'application/json'
    })
    .done((data) => {
      ServiceListAction.createSrvCatDir.completed(data.data);
    })
    .fail((error) => {
      ServiceListAction.createSrvCatDir.failed(error);
    })
  },
  onUpdateSrvCatDir(filter) {
    log("update:" + filter)
    $.ajax({
      url: API.UPDATE_SRVCATDIR,
      type:'post',
      data: JSON.stringify(filter),
      datatype:'json',
      contentType: 'application/json'
    })
    .done((data) => {
      ServiceListAction.updateSrvCatDir.completed(data.data);
    })
    .fail((error) => {
      ServiceListAction.updateSrvCatDir.failed(error);
    })
  },
  onDeleteSrvCatById(filter) {
    $.ajax({
      url: API.DELETE_SRVCAT_BY_ID,
      type:'post',
      data: JSON.stringify(filter),
      datatype:'json',
      contentType: 'application/json'
    })
    .done((data) => {
      ServiceListAction.deleteSrvCatById.completed(data);
    })
    .fail((error) => {
      ServiceListAction.deleteSrvCatById.failed(error);
    })
  }
});

